<?php

namespace App\Models;

use App\Libs\DataFormat;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Team extends Model
{

    use DataFormat;

    protected $table = 'team';
    protected $dates = [''];

    protected $guarded = ['id', 'created_at'];
    //protected $fillable = ['username', 'phone', 'pwd', 'avatars', 'type', 'org_id', 'token'];

    public static  $formatKey = ['id'=>'int', 'name'=>'string', 'type'=>'string', 'slogan'=>'string', 'logo'=>'image_url', 'team_photo'=>'json_decode',  'identifier'=>'string', 'contact_name'=>'string', 'contact_phone'=>'string', 'contact_email'=>'string', 'province_id'=>'int', 'province_name'=>'string', 'city_id'=>'int', 'city_name'=>'string', 'county_id'=>'int', 'county_name'=>'string', 'addr'=>'string', 'abstract'=>'string', 'reward_time'=>'string', 'team_size'=>'int', 'join_type'=>'int', 'created_at'=>'string', 'stars'=>'int'];

    public function getAllInfo()
    {
        $data = Team::format($this);

        $category = $this->category;
        $data['category'] = [];
        foreach($category as $value)
        {
            $data['category'][] = TeamCategory::format($value);
        }

        $serviceObj = $this->serviceObj;
        $data['service_object'] = [];
        foreach($serviceObj as $value)
        {
            $data['service_object'][] = TeamServiceObj::format($value);
        }

        foreach($data['photo'] as $key=>$value)
        {
            $data['photo'][$key] = image_url($value);
        }

        foreach($data['team_photo'] as $key=>$value)
        {
            $data['team_photo'][$key] = image_url($value);
        }
        return $data;
    }

    public function category()
    {
        return $this->hasMany('App\Models\TeamCategory', 'team_id', 'id');
    }

    public function serviceObj()
    {
        return $this->hasMany('App\Models\TeamServiceObj', 'team_id', 'id');
    }

    public function user()
    {
        return $this->hasOne('App\Models\User', 'id', 'user_id');
    }

    public function org()
    {
        return $this->hasOne('App\Models\Org', 'id', 'org_id');
    }


}
